#include <iostream>
#include <map>

struct Tmp {
    int a;
    std::string b;
    
    bool operator<(const Tmp &other) const {
        if (a < other.a) {
            return true;
        }
        if (b < other.b) {
            return true;
        }
        return false;
    }
};

int main() {
    std::map<std::string, int> map1 = {
            {"a", 1},
            {"b", 2},
            {"c", 3},
    };
    std::cout << "d" << " = " << map1["d"] << "\n";
    
    std::map<std::string, std::string> map2 = {
            {"a", "aa"},
            {"b", "bb"},
            {"c", "cc"},
    };
    std::cout << "d" << " = " << map2["d"] << "\n";
    
    // struct 作为 key
    auto key_a = Tmp{1, "a"};
    auto key_b = Tmp{2, "b"};
    auto key_c = Tmp{3, "c"};
    std::map<Tmp, int> map3 = {
            {Tmp{1, "a"}, 1},
            {Tmp{2, "b"}, 2},
            {Tmp{3, "c"}, 3},
    };
    map3.insert({Tmp{1, "a"}, 1}); // 与上面key相同，无效
    
    auto key = Tmp{3, "c"};
    auto noKey = Tmp{4, "d"}; // 不存在
    auto it = map3.find(key);
    
    std::cout << "map3.key_c []" << " = " << map3[key] << "\n";
    std::cout << "map3.key_c it" << " = " << it->second << "\n";
    std::cout << "map3.noKey   " << " = " << map3[noKey] << "\n";
    return 0;
}
